module oderogue.tcod.Random;

import derelict.tcod.libtcod;

public alias TCOD_random_algo_t RandomAlgorithm;

public struct Random
{
	private TCOD_random_t rand;
	
	public this(RandomAlgorithm algo)
	{
		rand = TCOD_random_new(algo);
	}
	
	public this(RandomAlgorithm algo, uint seed)
	{
		rand = TCOD_random_new_from_seed(algo, seed);
	}
	
	private this(TCOD_random_t rand)
	{
		this.rand = rand;
	}
	
	public ~this()
	{
		if (rand)
		{
			TCOD_random_delete(rand);
			rand = null;
		}
	}
	
	public static Random getDefaultGenerator()
	{
		return Random(TCOD_random_get_instance());
	}
	
	@property
	{
		public TCOD_random_t data() { return rand; }
	}
	
	public int getInt(int min, int max)
	{
		return TCOD_random_get_int(rand, min, max);
	}
	
	public float getFloat(float min, float max)
	{
		return TCOD_random_get_float(rand, min, max);
	}
	
	/**
	 * Get a random number, either integer or floating point, with an approximated Gaussian distribution.
	 */
	public float getGaussianFloat(float min, float max)
	{
		return TCOD_random_get_gaussian_float(rand, min, max);
	}
	
	/**
	 * Save the state of a generator.
	 */
	public Random save()
	{
		return Random(TCOD_random_save(rand));
	}
	
	/**
	 * Restore a saved random. This makes it possible to get the same serie of number several times with a single generator.
	 */
	public void restore(Random backup)
	{
		TCOD_random_restore(rand, backup.data);
	}
}

